<?php
$XS_MAP = array(	"A"	=> 0x0251,
			"{"	=> 0x00E6,
			"6"	=> 0x0250,
			"Q"	=> 0x0252,
			"E"	=> 0x025B,
			"@"	=> 0x0259,
			"3"	=> 0x025C,
			"I" 	=> 0x026A,
			"O" 	=> 0x0254,
			"2" 	=> 0x00F8,
			"9" 	=> 0x0153,
			"&" 	=> 0x0276,
			"U" 	=> 0x028A,
			"}" 	=> 0x0289,
			"V" 	=> 0x028C,
			"Y" 	=> 0x028F,
			"B" 	=> 0x03B2,
			"C" 	=> 0x00E7,
			"D" 	=> 0x00F0,
			"G" 	=> 0x0263,
			"L" 	=> 0x028E,
			"J" 	=> 0x0272,
			"N" 	=> 0x014B,
			"R" 	=> 0x0281,
			"S" 	=> 0x0283,
			"T" 	=> 0x03B8,
			"H" 	=> 0x0265,
			"Z" 	=> 0x0292,
			"?" 	=> 0x0294,
			":" 	=> 0x02D0,
			"\""	=> 0x02C8,
			"%" 	=> 0x02CC,
			"=" 	=> 0x0329,
			"~" 	=> 0x0303,
			"\'"	=> 0x02B2,
			"1" 	=> 0x0268,
			"8" 	=> 0x0275,
			"M" 	=> 0x026F,
			"7" 	=> 0x0264,
			"K" 	=> 0x026C,
			"5" 	=> 0x026B,
			"F" 	=> 0x0271,
			"4" 	=> 0x027E,
			"P" 	=> 0x028B,
			"W" 	=> 0x028D,
			"X" 	=> 0x03C7,
			"B\\" 	=> 0x0299,
			"G\\" 	=> 0x0262,
			"H\\"	=> 0x029C,
			"L\\"	=> 0x029F,
			"N\\" 	=> 0x0274,
			"R\\" 	=> 0x0280,
			"X\\" 	=> 0x0127,
			"?\\" 	=> 0x0295,
			"h\\" 	=> 0x0266,
			"J\\" 	=> 0x025F,
			"K\\" 	=> 0x026E,
			"p\\" 	=> 0x0278,
			"j\\" 	=> 0x029D,
			"M\\" 	=> 0x0270,
			"r\\" 	=> 0x0279,
			"@\\" 	=> 0x0258,
			"3\\" 	=> 0x025E,
			"l\\" 	=> 0x027A,
			"<\\" 	=> 0x02A2,
			">\\" 	=> 0x02A1,
			"s\\" 	=> 0x0255,
			"z\\" 	=> 0x0291,
			"x\\" 	=> 0x0267,
			"n`" 	=> 0x0273,
			"d`" 	=> 0x0256,
			"l`" 	=> 0x026D,
			"r`" 	=> 0x027B,
			"s`" 	=> 0x0282,
			"t`" 	=> 0x0288,
			"z`" 	=> 0x0290,
			"`" 	=> 0x02DE, 
			"_w" 	=> 0x02B7,
			"_G" 	=> 0x02E0,
			"_?\\" 	=> 0x02E4,
			"_h" 	=> 0x02B0, 
			"_>" 	=> 0x02BC, 
			"_?" 	=> 0x02BC,
			"_0" 	=> 0x0325,
			"_j" 	=> 0x02B2
);

function translate_xsampa($xsampa) {
	global $XS_MAP;
	$unicode = "";
	for ($lpos=0; $lpos<strlen($xsampa); $lpos++) {	
		for ($rpos=strlen($xsampa); $rpos>$lpos; $rpos--) {
			$map = substr($xsampa,$lpos,$rpos-$lpos);
			if (array_key_exists($map, $XS_MAP)) { 
				$unicode .= "&#" . $XS_MAP[$map]; 
				$lpos = $lpos + (strlen($map) - 1);
			}
			else if (strlen($map) == 1) { 
				$unicode .= $map;
			}
		}
	}
		$unicode = str_replace('_', '&#x0361', $unicode);

	return $unicode;	
}

function insert_xsampa_map($key, $value) {
	global $XS_MAP;
	$XS_MAP[$key] = $value;
}

if (isset($_GET['x'])) {
	print translate_xsampa($_GET['x']);
}
?>

